package com.pipayshop.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pipayshop.entity.AccountInfo;
import com.pipayshop.entity.UserInfo;
import com.pipayshop.entity.UserWithdrawal;
import com.pipayshop.entity.WithdrawalCommission;
import com.pipayshop.mapper.UserAccountInfoMapper;
import com.pipayshop.mapper.UserInfoMapper;
import com.pipayshop.mapper.UserWithdrawalMapper;
import com.pipayshop.mapper.WithdrawalCommissionMapper;
import com.pipayshop.service.UserWithdrawalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Service
public class UserWithdrawalServiceImpl extends ServiceImpl<UserWithdrawalMapper, UserWithdrawal> implements UserWithdrawalService {
    @Autowired
    private UserWithdrawalMapper userWithdrawalMapper;
    @Autowired
    private UserAccountInfoMapper userAccountInfoMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private WithdrawalCommissionMapper withdrawalCommissionMapper;

    @Override
    public List<UserWithdrawal> selectAll() {
        return userWithdrawalMapper.selectAll();
    }

    @Override
    public boolean updateBuyerDataById(UserWithdrawal userWithdrawal) {
        LocalDateTime now = LocalDateTime.now();
        userWithdrawal.setTimeOfApplication(now);
        userWithdrawal.setState(2);
        int i = userWithdrawalMapper.updateById(userWithdrawal);
        if (i != 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean insect(UserWithdrawal userWithdrawal) {
        LocalDateTime now = LocalDateTime.now();
        userWithdrawal.setTimeOfApplication(now);
        BigDecimal cashWithdrawalAmount = new BigDecimal(String.valueOf(userWithdrawal.getCashWithdrawalAmount()));
        Long userId = userWithdrawal.getUserId().longValue();
        // 获取用户uid
        UserInfo userInfo = userInfoMapper.selectByUserId(userId);
        userWithdrawal.setUId(userInfo.getUid());
        // 对用户账号进行扣减积分
        userAccountInfoMapper.updateByUid(userInfo.getUid(),cashWithdrawalAmount);
        int insert = userWithdrawalMapper.insert(userWithdrawal);
        if (insert != 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteById(Long id) {
        int i = userWithdrawalMapper.deleteByUserId(id);
        if (i != 0) {
            return true;
        }
        return false;
    }

    @Override
    public List<UserWithdrawal> selectByUserId(Integer userId) {
        return userWithdrawalMapper.selectByUserId(userId);
    }

    @Override
    public boolean updateByStateId(Long id) {
        UserWithdrawal withdrawal = userWithdrawalMapper.selectById(id);
        // 对用户账号进行添加积分
        userAccountInfoMapper.updateByAddUid(withdrawal.getUId(),withdrawal.getCashWithdrawalAmount());
        return userWithdrawalMapper.updateByState(id);
    }
}
